package com.itheima.recommend.service.impl;

import com.itheima.entity.Result;
import com.itheima.entity.StatusCode;
import com.itheima.feign.ProductFeign;
import com.itheima.pojo.Product;

import com.itheima.recommend.pojo.Graph_product;
import com.itheima.recommend.service.RecommendService;
import com.itheima.recommend.util.SmartSearchUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class RecommendServiceImpl implements RecommendService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    ProductFeign productFeign;

    public void init() {

        Integer count = (int) productFeign.count().getData();
        if (count == 0) {
            throw new RuntimeException("没有商品");
        }
        // 搜索记录
        Graph_product graph_comment = SmartSearchUtil.build(count);

        List<Product> list = productFeign.findAll();

        for (Product product : list) {
            //创建顶点
            SmartSearchUtil.addRoot(graph_comment, product.getName());
        }
//        redisTemplate.boundHashOps("Smart").put("history", graph_comment);
        SmartSearchUtil.graph_product = graph_comment;
    }

    @Override
    public void addEdge(String p1, String p2) {

//        Graph_product graph = (Graph_product) redisTemplate.boundHashOps("Smart").get("history");
        Graph_product graph = SmartSearchUtil.graph_product;
        SmartSearchUtil.addEdge(graph, p1, p2);
    }

    @Override
    public List<String> smartSearch(String keyword, Integer number) {
        if (number == null) {
            number = 4;
        }
        return SmartSearchUtil.smart(SmartSearchUtil.graph_product, number, keyword);
    }

}
